package com.wefiesta.app;

import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.wefiesta.app.http.LoginTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	private String TAG = "LoginActivity";
	private String loginURL = "http://eduword.starstory.net/load/login.do";

	private Context mContext = null;

	private EditText id;
	private EditText password;

	private CheckBox autoLogin;

	private ProgressDialog mProgressDialog;

	private String userid;
	private String userpw;

	private boolean setAutoLogin;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		

		mContext = this;

		id = (EditText) findViewById(R.id.id);
		password = (EditText) findViewById(R.id.password);

		// 로그인 버튼
		findViewById(R.id.login).setOnClickListener(mNowLoginBtnListener);
	}


	public String[] inputInvalid() {

		String[] result = new String[2];

		userid = id.getText().toString();
		userpw = password.getText().toString();

		result[0] = userid.length() != 0 ? "완료" : "아이디를 입력해주세요";
		result[1] = userpw.length() != 0 ? "완료" : "비밀번호를 입력해주세요";

		return result;

	}
	
	
	private OnClickListener mNowLoginBtnListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			String[] inputResult = inputInvalid();
			int resultCount = 0;
			for (int i = 0; i < inputResult.length; i++) {

				if (!inputResult[i].equals("완료")) {

					Toast.makeText(getApplicationContext(), inputResult[i],
							Toast.LENGTH_SHORT).show();
					resultCount++;
				}
			}

			if (resultCount == 0) {
				// Login 수행

				if (setAutoLogin) {
					SharedPreferences pref = getSharedPreferences("autoLogin",
							MODE_PRIVATE);

					SharedPreferences.Editor editor = pref.edit();
					editor.putBoolean("autologin", true);
					editor.putString("userid", userid);
					editor.putString("userpw", userpw);
					editor.commit();
				}
				LoginTask loginTask = new LoginTask(LoginActivity.this);
				loginTask.mContext = getApplicationContext();
				loginTask.execute(loginURL, userid, userpw);

			}
		}
	};

}